package io.renren.modules.sq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sq.dao.SqRoleSystemDao;
import io.renren.modules.sq.entity.SqRoleSystemEntity;
import io.renren.modules.sq.entity.SqSystemEditionEntity;
import io.renren.modules.sq.entity.SqSystemEntity;
import io.renren.modules.sq.service.SqRoleSystemService;
import io.renren.modules.sq.service.SqSystemEditionService;
import io.renren.modules.sq.service.SqSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Service("sqRoleSystemService")
public class SqRoleSystemServiceImpl extends ServiceImpl<SqRoleSystemDao, SqRoleSystemEntity> implements SqRoleSystemService {

    @Autowired
    SqSystemService sqSystemService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SqRoleSystemEntity> page = this.page(
                new Query<SqRoleSystemEntity>().getPage(params),
                new QueryWrapper<SqRoleSystemEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public boolean addRoleSystemrule(List<Long> ids, Long roleId) {
        Set<Long> ids_set = new HashSet<>();
        for (Long id : ids) {
            ids_set.add(id);
            this.baseMapper.addRoleSystemrule(id,roleId);
        }
        List<SqSystemEntity> sqSystemEntities = sqSystemService.selectListByRoleid(roleId);
        for (SqSystemEntity one:sqSystemEntities) {
            if(!ids_set.contains(one.getId()))
                this.baseMapper.delete(new QueryWrapper<SqRoleSystemEntity>().eq("system_id",one.getId()).eq("role_id",roleId));
        }
        return true;
    }

}
